#include "ipc_common.hpp"
#include <iostream>
#include <string>

int main() {
    std::cout << "启动IPC客户端...\n";

    // 连接到命名管道
    HANDLE pipe = CreateFileA(
        PIPE_NAME,
        GENERIC_READ | GENERIC_WRITE,
        0,
        nullptr,
        OPEN_EXISTING,
        0,
        nullptr
    );

    if (pipe == INVALID_HANDLE_VALUE) {
        std::cerr << "连接到服务器失败. 错误码: " << GetLastError() << std::endl;
        return 1;
    }

    std::cout << "已连接到服务器\n";

    // 主循环
    std::string input;
    while (true) {
        std::cout << "请输入消息 (输入'exit'退出): ";
        std::getline(std::cin, input);

        if (input == "exit") {
            break;
        }

        // 发送消息
        if (!WriteMessage(pipe, input)) {
            std::cerr << "发送消息失败\n";
            break;
        }

        // 接收响应
        std::string response = ReadMessage(pipe);
        if (response.empty()) {
            std::cerr << "接收响应失败\n";
            break;
        }

        std::cout << "服务器响应: " << response << std::endl;
    }

    // 清理
    CloseHandle(pipe);
    return 0;
}
